package ph.com.gs3.formalistics.model.ldao;

import ph.com.gs3.formalistics.global.constants.StarMark;
import ph.com.gs3.formalistics.global.utilities.logging.FLLogger;
import ph.com.gs3.formalistics.model.LocalDataAccessObject;
import ph.com.gs3.formalistics.model.tables.UserDocumentsTable;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

public class UserDocumentsLDAO extends LocalDataAccessObject {

	public static final String TAG = UserDocumentsLDAO.class.getSimpleName();

	public UserDocumentsLDAO(Context context) {
		super(context);
	}

	public void insertUserDocument(int userId, int documentId, StarMark starMark) {

		ContentValues values = createCVFromData(userId, documentId, starMark);
		database.insert(UserDocumentsTable.NAME, null, values);

	}

	public void changeDocumentStarMark(int documentId, StarMark starMark) {

		ContentValues cv = new ContentValues();
		cv.put(UserDocumentsTable.COL_IS_STARRED, starMark == StarMark.STARRED ? 1 : 0);

		String whereClause = UserDocumentsTable.COL_DOCUMENT_ID + "=?";
		String[] whereArgs = { Integer.toString(documentId) };

		long affectedRows = database.update(UserDocumentsTable.NAME, cv, whereClause, whereArgs);

		if (affectedRows <= 0) {
			FLLogger.e(TAG, "Failed to mark document " + documentId + " as starred/unstarred");
		} else if (affectedRows > 1) {
			FLLogger.e(TAG, "More than one documents are starred/unstarred by marking document "
			        + documentId);
		}

	}

	public void changeDocumentOutgoingMark(int documentId, boolean isOutgoing) {

		ContentValues cv = new ContentValues();
		cv.put(UserDocumentsTable.COL_IS_OUTGOING, isOutgoing ? 1 : 0);

		String whereClause = UserDocumentsTable.COL_DOCUMENT_ID + "=?";
		String[] whereArgs = { Integer.toString(documentId) };

		long affectedRows = database.update(UserDocumentsTable.NAME, cv, whereClause, whereArgs);

		if (affectedRows <= 0) {
			FLLogger.e(TAG, "Failed to mark document " + documentId + " as outgoing/normal");
		} else if (affectedRows > 1) {
			FLLogger.e(TAG, "More than one documents are marked as outgoing/normal by document "
			        + documentId);
		}

	}

	public int getUserOfDocument(int documentId) {

		String whereClause = UserDocumentsTable.COL_DOCUMENT_ID + "=?";
		String[] whereArgs = { Integer.toString(documentId) };
		String[] columns = { UserDocumentsTable.COL_USER_ID };

		Cursor cursor = database.query(UserDocumentsTable.NAME, columns, whereClause, whereArgs,
		        null, null, null);
		cursor.moveToFirst();

		int userId = 0;

		if (!cursor.isAfterLast()) {
			int index = cursor.getColumnIndexOrThrow(UserDocumentsTable.COL_USER_ID);
			userId = cursor.getInt(index);
		}

		cursor.close();
		return userId;

	}

	public int getUserDocumentsCount(int userId) {

		String whereClause = UserDocumentsTable.COL_USER_ID + "=?";
		String[] whereArgs = { Integer.toString(userId) };
		String[] columns = { UserDocumentsTable.COL_USER_ID };

		Cursor cursor = database.query(UserDocumentsTable.NAME, columns, whereClause, whereArgs,
		        null, null, null);
		int count = cursor.getCount();
		cursor.close();

		return count;

	}

	public ContentValues createCVFromData(int userId, int documentId, StarMark starMark) {

		int starMarkInt = (starMark == StarMark.STARRED) ? 1 : 0;

		ContentValues cv = new ContentValues();

		cv.put(UserDocumentsTable.COL_USER_ID, userId);
		cv.put(UserDocumentsTable.COL_DOCUMENT_ID, documentId);
		cv.put(UserDocumentsTable.COL_IS_STARRED, starMarkInt);

		return cv;

	}

}
